package su.validator.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class VerifiableMethodValidationInterceptor implements MethodInterceptor {
    private final ValidateService validateService;
    public VerifiableMethodValidationInterceptor(ValidateService validateService) {
        if (Objects.isNull(validateService)) {
            throw new IllegalArgumentException("缺少校验服务");
        }
        this.validateService = validateService;

    }
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Object[] arguments = invocation.getArguments();
        if (arguments.length > 0) {
            List<Object> params = new ArrayList<>();
            for (Object argument : arguments) {
                params.add(argument);
            }
            this.validateService.validateParam(params);
        }
        return invocation.proceed();
    }
}
